var orthodonticsDetail = {
    id: "orthodonticsDetailTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

orthodonticsDetail.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerSex', field: 'customerSex', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerAge', field: 'customerAge', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerSexHead', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter:function (value, row, index) {
                return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            }
        },
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '交易类型', field: 'checkoutMethod', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '收费方式', field: 'modeName', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '金额', field: 'moneyBalance', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '入会时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true,width: 130,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '结束时间', field: 'endTime', align: 'center', valign: 'middle', sortable: true,width: 130,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 130},
    ]
    return columns;
};

orthodonticsDetail.formParams = function () {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['fieldTc'] = $('#fieldTc').val();
    queryData['customerStatus'] = $('#customerStatus').val();
    queryData['reach'] = $("#doctorTcInputPublics", parent.document).val();
    return queryData;
};

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

orthodonticsDetail.downLoad = function () {
    var queryData = {};
    queryData['reach'] = $("#doctorTcInputPublics", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/orthodonticsDetail/paymentExport?' + param;
};

orthodonticsDetail.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        orthodonticsDetail.seItem = selected[0];
        return true;
    }
};

orthodonticsDetail.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

orthodonticsDetail.resetSearch = function () {
    $("#startTime").val("");
    $("#endTime").val("");
    $("#fieldTc").val("");
    $("#customerStatus").val("");
    $("#doctorTcInputPublics", parent.document).val("");
    orthodonticsDetail.search();
};

orthodonticsDetail.orthodonticsDetailExport = function () {
    var param = parseParam(orthodonticsDetail.formParams());
    location.href = Feng.ctxPath + '/orthodonticsDetail/orthodonticsDetailExport?' + param;
};

orthodonticsDetail.search = function () {
    orthodonticsDetail.table.refresh({query: orthodonticsDetail.formParams()});
    orthodonticsDetail.searchTotal();
};

orthodonticsDetail.searchTotal = function () {//金额统计
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/orthodonticsDetail/sumorthodonticsDetail',
        data: orthodonticsDetail.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html("(" + data.borrowAmount_sum + ")元");
        }
    });
};

$("#doctorTcSearchAlls", parent.document).click(function () {//主页搜索
    orthodonticsDetail.search();
});

orthodonticsDetail.today = function () {//今日
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    var dates = new Date();
    dates.setDate(dates.getDate() - 1);
    var currentYears = dates.getFullYear();
    var currentMonths = dates.getMonth() + 1;
    var currentDays = dates.getDate();
    if (currentMonths < 10) currentMonths = "0" + currentMonths;
    if (currentDays < 10) currentDays = "0" + currentDays;
    if($("#naturalDaySearch").val() == "1"){
        $("#startTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00");
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
    }else {
        $("#startTime").val(currentYears + "-" + currentMonths + "-" + currentDays + " " + $("#sttleAccountsTime").val());
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + $("#sttleAccountsTime").val());
    }
    $("#timeValue").val('1');
    orthodonticsDetail.search();
};

orthodonticsDetail.yesterday = function () {//昨日
    var date = new Date();
    date.setDate(date.getDate() - 1);
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    var dates = new Date();
    dates.setDate(dates.getDate() - 2);
    var currentYears = dates.getFullYear();
    var currentMonths = dates.getMonth() + 1;
    var currentDays = dates.getDate();
    if (currentMonths < 10) currentMonths = "0" + currentMonths;
    if (currentDays < 10) currentDays = "0" + currentDays;
    if($("#naturalDaySearch").val() == "1"){
        $("#startTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00");
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
    }else {
        $("#startTime").val(currentYears + "-" + currentMonths + "-" + currentDays + " " + $("#sttleAccountsTime").val());
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + $("#sttleAccountsTime").val());
    }
    $("#timeValue").val('2');
    orthodonticsDetail.search();
};

orthodonticsDetail.thisWeek = function () {//本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    var weekTimeStartYear = monday.getFullYear();
    var weekTimeStartMonth = monday.getMonth() + 1;
    var weekTimeStartDay = monday.getDate();
    if (weekTimeStartMonth < 10) weekTimeStartMonth = "0" + weekTimeStartMonth;
    if (weekTimeStartDay < 10) weekTimeStartDay = "0" + weekTimeStartDay;
    var weekTime = new Date(monday);
    weekTime.setDate(weekTime.getDate() - 1);
    var weekTimeYear = weekTime.getFullYear();
    var weekTimeMonth = weekTime.getMonth() + 1;
    var weekTimeDay = weekTime.getDate();
    if (weekTimeMonth < 10) weekTimeMonth = "0" + weekTimeMonth;
    if (weekTimeDay < 10) weekTimeDay = "0" + weekTimeDay;
    var weekTimeEndYear = sunday.getFullYear();
    var weekTimeEndMonth = sunday.getMonth() + 1;
    var weekTimeEndDay = sunday.getDate();
    if (weekTimeEndMonth < 10) weekTimeEndMonth = "0" + weekTimeEndMonth;
    if (weekTimeEndDay < 10) weekTimeEndDay = "0" + weekTimeEndDay;
    if($("#naturalDaySearch").val() == "1"){
        $("#startTime").val(weekTimeStartYear + "-" + weekTimeStartMonth + "-" + weekTimeStartDay + " " + "00:00:00");
        $("#endTime").val(weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + "23:59:59");
    }else {
        $("#startTime").val(weekTimeYear + "-" + weekTimeMonth + "-" + weekTimeDay + " " + $("#sttleAccountsTime").val());
        $("#endTime").val(weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + $("#sttleAccountsTime").val());
    }
    $("#timeValue").val('3');
    orthodonticsDetail.search();
};

orthodonticsDetail.thisMonth = function () {//本月
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    var currentDay = 28;
    if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12) {
        currentDay = 31;
    } else if (currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11) {
        currentDay = 30;
    }
    var dates = new Date();
    dates.setMonth(dates.getMonth() - 1, 1);
    var currentYears = dates.getFullYear();
    var currentMonths = dates.getMonth() + 1;
    if (currentMonths < 10) currentMonths = "0" + currentMonths;
    var currentDays = 28;
    if (currentMonths == 1 || currentMonths == 3 || currentMonths == 5 || currentMonths == 7 || currentMonths == 8 || currentMonths == 10 || currentMonths == 12) {
        currentDays = 31;
    } else if (currentMonths == 4 || currentMonths == 6 || currentMonths == 9 || currentMonths == 11) {
        currentDays = 30;
    }

    if($("#naturalDaySearch").val() == "1"){
        $("#startTime").val(currentYear + "-" + currentMonth + "-" + "01" + " " + "00:00:00");
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
    }else {
        $("#startTime").val(currentYears + "-" + currentMonths + "-" + currentDays + " " + $("#sttleAccountsTime").val());
        $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + $("#sttleAccountsTime").val());
    }
    $("#timeValue").val('4');
    orthodonticsDetail.search();
};

orthodonticsDetail.naturalDaySearch = function () {//自然时间
    $("#naturalDaySearch").val('1');
    if($("#timeValue").val() == "1"){
        var date1 = new Date();
        var currentYear1 = date1.getFullYear();       // 当前年份
        var currentMonth1 = date1.getMonth() + 1;    // 当前月份
        var currentDay1 = date1.getDate();         // 今日日期 几号
        if (currentMonth1 < 10) currentMonth1 = "0" + currentMonth1;
        if (currentDay1 < 10) currentDay1 = "0" + currentDay1;
        $("#startTime").val(currentYear1 + "-" + currentMonth1 + "-" + currentDay1 + " " + "00:00:00");
        $("#endTime").val(currentYear1 + "-" + currentMonth1 + "-" + currentDay1 + " " + "23:59:59")
    }else if($("#timeValue").val() == "2"){
        var date3 = new Date();
        date3.setDate(date3.getDate() - 1);
        var currentYear3 = date3.getFullYear();       // 当前年份
        var currentMonth3 = date3.getMonth() + 1;    // 当前月份
        var currentDay3 = date3.getDate();         // 今日日期 几号
        if (currentMonth3 < 10) currentMonth3 = "0" + currentMonth3;
        if (currentDay3 < 10) currentDay3 = "0" + currentDay3;
        $("#startTime").val(currentYear3 + "-" + currentMonth3 + "-" + currentDay3 + " " + "00:00:00");
        $("#endTime").val(currentYear3 + "-" + currentMonth3 + "-" + currentDay3 + " " + "23:59:59")
    }else if($("#timeValue").val() == "3"){
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {stepSunDay = -7;}
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        var weekTimeStartYear = monday.getFullYear();       // 当前年份
        var weekTimeStartMonth = monday.getMonth() + 1;    // 当前月份
        var weekTimeStartDay = monday.getDate();         // 今日日期 几号
        if (weekTimeStartMonth < 10) weekTimeStartMonth = "0" + weekTimeStartMonth;
        if (weekTimeStartDay < 10) weekTimeStartDay = "0" + weekTimeStartDay;
        var weekTimeEndYear = sunday.getFullYear();       // 当前年份
        var weekTimeEndMonth = sunday.getMonth() + 1;    // 当前月份
        var weekTimeEndDay = sunday.getDate();         // 今日日期 几号
        if (weekTimeEndMonth < 10) weekTimeEndMonth = "0" + weekTimeEndMonth;
        if (weekTimeEndDay < 10) weekTimeEndDay = "0" + weekTimeEndDay;
        $("#startTime").val(weekTimeStartYear + "-" + weekTimeStartMonth + "-" + weekTimeStartDay + " " + "00:00:00");
        $("#endTime").val(weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + "23:59:59")
    }if($("#timeValue").val() == "4"){
        var date5 = new Date();
        var currentYear5 = date5.getFullYear();       // 当前年份
        var currentMonth5 = date5.getMonth() + 1;    // 当前月份
        if (currentMonth5 < 10) currentMonth5 = "0" + currentMonth5;
        var currentDay5 = 28;
        if (currentMonth5 == 1 || currentMonth5 == 3 || currentMonth5 == 5 || currentMonth5 == 7 || currentMonth5 == 8 || currentMonth5 == 10 || currentMonth5 == 12) {
            currentDay5 = 31;
        } else if (currentMonth5 == 4 || currentMonth5 == 6 || currentMonth5 == 9 || currentMonth5 == 11) {
            currentDay5 = 30;
        }
        $("#startTime").val(currentYear5 + "-" + currentMonth5 + "-" + "01" + " " + "00:00:00");
        $("#endTime").val(currentYear5 + "-" + currentMonth5 + "-" + currentDay5 + " " + "23:59:59")
    }
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    orthodonticsDetail.search();
};

orthodonticsDetail.sttleAccountsSearch = function () {//扎帐时间
    $("#naturalDaySearch").val('2');
    var AccountsTime = $("#sttleAccountsTime").val();
    if($("#timeValue").val() == "1"){
        var date1 = new Date();
        var currentYear1 = date1.getFullYear();       // 当前年份
        var currentMonth1 = date1.getMonth() + 1;    // 当前月份
        var currentDay1 = date1.getDate();         // 今日日期 几号
        if (currentMonth1 < 10) currentMonth1 = "0" + currentMonth1;
        if (currentDay1 < 10) currentDay1 = "0" + currentDay1;
        var date2 = new Date();
        date2.setDate(date2.getDate() - 1);
        var currentYears2 = date2.getFullYear();       // 当前年份
        var currentMonths2 = date2.getMonth() + 1;    // 当前月份
        var currentDays2 = date2.getDate();         // 今日日期 几号
        if (currentMonths2 < 10) currentMonths2 = "0" + currentMonths2;
        if (currentDays2 < 10) currentDays2 = "0" + currentDays2;
        $("#startTime").val(currentYears2 + "-" + currentMonths2 + "-" + currentDays2 + " " + AccountsTime);
        $("#endTime").val(currentYear1 + "-" + currentMonth1 + "-" + currentDay1 + " " + AccountsTime)
    }else if($("#timeValue").val() == "2"){
        var date3 = new Date();
        date3.setDate(date3.getDate() - 1);
        var currentYear3 = date3.getFullYear();       // 当前年份
        var currentMonth3 = date3.getMonth() + 1;    // 当前月份
        var currentDay3 = date3.getDate();         // 今日日期 几号
        if (currentMonth3 < 10) currentMonth3 = "0" + currentMonth3;
        if (currentDay3 < 10) currentDay3 = "0" + currentDay3;
        var date4 = new Date();
        date4.setDate(date4.getDate() - 2);
        var currentYear4 = date4.getFullYear();       // 当前年份
        var currentMonth4 = date4.getMonth() + 1;    // 当前月份
        var currentDay4 = date4.getDate();         // 今日日期 几号
        if (currentMonth4 < 10) currentMonth4 = "0" + currentMonth4;
        if (currentDay4 < 10) currentDay4 = "0" + currentDay4;
        $("#startTime").val(currentYear4 + "-" + currentMonth4 + "-" + currentDay4 + " " + AccountsTime);
        $("#endTime").val(currentYear3 + "-" + currentMonth3 + "-" + currentDay3 + " " + AccountsTime)
    }else if($("#timeValue").val() == "3"){
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {
            stepSunDay = -7;
        }
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        var weekTime = new Date(monday);
        weekTime.setDate(weekTime.getDate() - 1);
        var weekTimeYear = weekTime.getFullYear();
        var weekTimeMonth = weekTime.getMonth() + 1;
        var weekTimeDay = weekTime.getDate();
        if (weekTimeMonth < 10) weekTimeMonth = "0" + weekTimeMonth;
        if (weekTimeDay < 10) weekTimeDay = "0" + weekTimeDay;
        var weekTimeEndYear = sunday.getFullYear();       // 当前年份
        var weekTimeEndMonth = sunday.getMonth() + 1;    // 当前月份
        var weekTimeEndDay = sunday.getDate();         // 今日日期 几号
        if (weekTimeEndMonth < 10) weekTimeEndMonth = "0" + weekTimeEndMonth;
        if (weekTimeEndDay < 10) weekTimeEndDay = "0" + weekTimeEndDay;
        $("#startTime").val(weekTimeYear + "-" + weekTimeMonth + "-" + weekTimeDay + " " + AccountsTime);
        $("#endTime").val(weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + AccountsTime)
    }if($("#timeValue").val() == "4"){
        var date5 = new Date();
        var currentYear5 = date5.getFullYear();       // 当前年份
        var currentMonth5 = date5.getMonth() + 1;    // 当前月份
        if (currentMonth5 < 10) currentMonth5 = "0" + currentMonth5;
        var currentDay5 = 28;
        if (currentMonth5 == 1 || currentMonth5 == 3 || currentMonth5 == 5 || currentMonth5 == 7 || currentMonth5 == 8 || currentMonth5 == 10 || currentMonth5 == 12) {
            currentDay5 = 31;
        } else if (currentMonth5 == 4 || currentMonth5 == 6 || currentMonth5 == 9 || currentMonth5 == 11) {
            currentDay5 = 30;
        }
        var date6 = new Date();
        date6.setMonth(date6.getMonth() - 1, 1);
        var currentYears6 = date6.getFullYear();       // 当前年份
        var currentMonths6 = date6.getMonth() + 1;    // 上个月份
        if (currentMonths6 < 10) currentMonths6 = "0" + currentMonths6;
        var currentDays6 = 28;
        if (currentMonths6 == 1 || currentMonths6 == 3 || currentMonths6 == 5 || currentMonths6 == 7 || currentMonths6 == 8 || currentMonths6 == 10 || currentMonths6 == 12) {
            currentDays6 = 31;
        } else if (currentMonths6 == 4 || currentMonths6 == 6 || currentMonths6 == 9 || currentMonths6 == 11) {
            currentDays6 = 30;
        }
        $("#startTime").val(currentYears6 + "-" + currentMonths6 + "-" + currentDays6 + " " + AccountsTime);
        $("#endTime").val(currentYear5 + "-" + currentMonth5 + "-" + currentDay5 + " " + AccountsTime)
    }
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    orthodonticsDetail.search();
};
orthodonticsDetail.orthodonticsReferee = function () {
    if (this.check()) {
        var id = orthodonticsDetail.seItem.id;
        var index = layer.open({
            type: 2,
            title: '关联推荐人',
            area: ['60%', '80%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/orthodonticsDetail/orthodonticsReferee/' + id
        });
        this.layerIndex = index
    }
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#orthodonticsDetailTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = orthodonticsDetail.initColumn();
    var table = new BSTable(orthodonticsDetail.id, "/orthodonticsDetail/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(orthodonticsDetail.formParams());
    orthodonticsDetail.table = table.init();
    orthodonticsDetail.today();
    checkDate = function () {orthodonticsDetail.search();};
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {orthodonticsDetail.search();});
    phonePrivileges();
});